<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Activity\Mapper;

use App\Activity\Model\ActivityFirst;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 新人奖励设置Mapper类
 */
class ActivityFirstMapper extends AbstractMapper
{
    /**
     * @var ActivityFirst
     */
    public $model;

    public function assignModel()
    {
        $this->model = ActivityFirst::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 1:产品，2:金币
        if (!empty($params['award_type'])) {
            $query->where('award_type', '=', $params['award_type']);
        }

        // 产品名称
        if (!empty($params['product_name'])) {
            $query->where('product_name', 'like', '%' . $params['product_name'] . '%');
        }

        // 创建时间
        if (!empty($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        return $query;
    }

    public function saveProduct($params)
    {
        if (empty($params['product_id'][0])) {
            throw new \ErrorException('请选择商品');
        }
        $productName = \App\Config\Model\ConfigProduct::query()->where('id', $params['product_id'][0])->value('name');
        $this->model::query()->where('id', 1)->update(['award_value' => $params['product_id'][0], 'product_name' => $productName]);

        return $params['product_id'][0];
    }

    public function saveMoney($params)
    {
        if (empty($params['money'])) {
            throw new \ErrorException('请输入金额');
        }
        $this->model::query()->where('id', 2)->update(['award_value' => $params['money'] * \App\Constants\AdminEnum::PRICE_MULTIPLE]);
        return $params['money'];
    }

    public function getInitData($params)
    {
        $list = $this->model::query()->get();
        $res = [];
        $productId = '';
        foreach ($list as $item) {
            if ($item->award_type == 1) {
                $productId = $item->award_value;
            }
            if ($item->award_type == 2) {
                $money = $item->award_value;
            }
        }
        //实时领取
        if (empty($productId)) {
            $res = [
                'product_id' => 0,
                'money' => empty($money) ? 0 : $money,
                'receive_num' => 0,
                'picked_up_not' => 0,
                'zbt_store' => 0,
                'zbt_price' => 0,
                'product_price' => 0,
                'product_amount_price' => 0
            ];
        }
        $receiveCount = 0;//\App\Record\Model\RecordUserWelfare::query()->where('type', 1)->where('product_id', $productId)->count();
        //背包来源:18
        $pickedUpNotCount = 0;
//         \App\User\Model\UserBag::query()
//            ->where('source_type', 18)
//            ->where('product_id', $productId)
//            ->where('status', 0)
//            ->count();

        //获取产品
        $productInfo = \App\Config\Model\ConfigProduct::query()->where('id', $productId)->first();
        $postRes = [];
        if (!empty($productInfo)) {
            $markName[] = $productInfo->english_name;
            $postRes = \App\Lib\Library\ZbtQuery::queryPriceBathName($markName);
        }else{
            return [];
        }


        $res = [
            'product_id' => $productId,
            'money' => empty($money) ? 0 : $money,
            'receive_num' => empty($receiveCount) ? 0 : $receiveCount,
            'picked_up_not' => empty($pickedUpNotCount) ? 0 : $pickedUpNotCount,
            'zbt_store' => empty($postRes[$productInfo->item_id]['quantity']) ? 0 : $postRes[$productInfo->item_id]['quantity'],
            'zbt_price' => empty($postRes[$productInfo->item_id]['price']) ? 0 : $postRes[$productInfo->item_id]['price'],
            'product_price' => $productInfo->price,
            'product_amount_price' => $productInfo->amount_price,
        ];
        return $res;
    }
}